package dao.impl;

import dao.CollectDao;
import domian.Collect;
import utils.BaseDAO;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 收藏
 */
public class CollectDaoImpl implements CollectDao {


    @Override
    public Integer addCollect(Collect collect) {
        String sql ="insert into  collect(uid,fid,time) values(?,?,?)";
        Object[] arr={collect.getUid(),collect.getFid(),collect.getTime()};
        return BaseDAO.executeUpdate(sql,arr);
    }

    @Override
    public Integer cancelCollect(Collect collect) {
        String sql="delete from collect where fid = ?";
        Object[] arr={collect.getFid()};
        return BaseDAO.executeUpdate(sql,arr);
    }

    @Override
    public List<Collect> viewCollect(Integer uid, Integer rows, Integer pages) {
        Connection connection =null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        List<Collect> list = new ArrayList<>();
        try {
            //获取连接
            connection = BaseDAO.getConnection();
            //创建sql语句
            String sql="SELECT realname,id,time,f_id,f_name,f_userId,f_integral FROM (\n" +
                    " SELECT id,time,f_id,f_name,f_userId,f_integral FROM collect LEFT JOIN file ON collect.`fid`=file.`f_id` WHERE uid=\n"+uid +
                    ") AS subquery JOIN user ON subquery.f_userId = user.uid";
            if (rows != null && pages != null) {
                sql += " LIMIT "+rows+","+pages;
            }
            //获取sql语句
            ps = connection.prepareStatement(sql);
            //执行sql语句
            resultSet = ps.executeQuery();
            while (resultSet.next()){
                Integer fId = resultSet.getInt("f_id");
                String realname = resultSet.getString("realname"); //作者名字
                String fName = resultSet.getString("f_name"); //文件名字
                Integer fIntegral = resultSet.getInt("f_integral"); //积分
                Timestamp time = resultSet.getTimestamp("time"); //添加注册时间

                list.add(new Collect(fId,time,fName,realname,fIntegral));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭资源
            BaseDAO.close(connection,ps,resultSet);
        }
        return list;
    }

    @Override
    public List<Collect> fidCollect(Integer uid) {
        Connection connection =null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        List<Collect> list = new ArrayList<>();
        try {
            //获取连接
            connection = BaseDAO.getConnection();
            //创建sql语句
            String sql="SELECT fid FROM collect WHERE uid=?";

            //获取sql语句
            ps = connection.prepareStatement(sql);
            ps.setInt(1,uid);
            //执行sql语句
            resultSet = ps.executeQuery();
            while (resultSet.next()){
                Integer fId = resultSet.getInt("fid");


                list.add(new Collect(fId));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭资源
            BaseDAO.close(connection,ps,resultSet);
        }
        return list;
    }

}
